var xhr;
var start;
var pre;
var nodProducts;
var img;
function browseProduct(choice)
{   
    if (window.ActiveXObject)
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    else if (window.XMLHttpRequest)
        xhr = new XMLHttpRequest;
   start=0;
    xhr.onreadystatechange = function() {handleBrowseResults(0);}
    xhr.open("GET", "BrowseServlet?prod_id="+choice, true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlendoded");
    xhr.send(null);

}
/*---------------------------------------------------------------------------*/
function handleBrowseResults(choice)
{
    if (xhr.readyState != 4) return;
    if (xhr.status != 200) {
        alert(xhr.status); // for troubleshooting
        return;
    }

    var xhresponse = xhr.responseXML;
    nodProducts = xhresponse.getElementsByTagName("Product");
    var nodProduct;
    j=0;
    if (choice==1) start = pre;
     for (i=start; (i < nodProducts.length) && (j<6); i++,j++) {
        nodProduct = nodProducts[i];

	
        strLink = nodProduct.getElementsByTagName("Link")[0].firstChild.nodeValue;
        strElementId = "P"+(i%6+1).toString();
        document.getElementById(strElementId).setAttribute("name",i);
       
        document.getElementById(strElementId).setAttribute("src", strLink);
        }
        start = i;
        pre = i-j-6;
       
        if (i<nodProducts.length) showup("Next"); else hide("Next");
        if (i>6) showup("Pre"); else hide("Pre");
}
/*---------------------------------------------------------------------------*/
function showup(buttonId)
{
 document.getElementById(buttonId).style.display='inline';
}
/*---------------------------------------------------------------------------*/
function hide(buttonId)
{
 document.getElementById(buttonId).style.display='none';
}
/*---------------------------------------------------------------------------*/
function getProduct(image) {
    i=image.name;
    img = image;
    nodProduct = nodProducts[i];
        strDescription="testing";
        strID = nodProduct.getElementsByTagName("ProductID")[0].firstChild.nodeValue;
        strName = nodProduct.getElementsByTagName("ProductName")[0].firstChild.nodeValue;
        strDescription = nodProduct.getElementsByTagName("Description")[0].firstChild.nodeValue;
        strPrice = nodProduct.getElementsByTagName("Price")[0].firstChild.nodeValue;
        strUnits = nodProduct.getElementsByTagName("Units")[0].firstChild.nodeValue;
  
    putData(document.getElementById("tdProductName"), strName);
    putData(document.getElementById("tdDescription"), strDescription);
    putData(document.getElementById("tdProductID"), strID);
    putData(document.getElementById("tdPrice"), strPrice);
    putData(document.getElementById("tdUnits"), strUnits);

    //alert(strName);
    showTooltip();
}
/*****************************************************************************/
function putData(cell, data) {
    removeChildren(cell);
    cell.appendChild(document.createTextNode(data));
}
/*---------------------------------------------------------------------------*/
function showTooltip() {

    /* find where to display the tooltip */
    var x = getPosition(img, "offsetLeft") - 250;
    var y = getPosition(img, "offsetTop");

    /* display the tooltip */
    var divTooltip = document.getElementById("divTooltip");

    divTooltip.style.left = x + 'px';
    divTooltip.style.top = y + 'px';

    divTooltip.style.display = "block";
}
/*---------------------------------------------------------------------------*/
function hideTooltip() {

    /* hide the tooltip */
    document.getElementById("divTooltip").style.display = "none";
}